﻿using MediatR;
using SqlSugar;
using WarehouseManagement.Application.Command.WarehouseSAppCommand;
using WarehouseManagement.Domain.DTO.WarehouseSApp;
using WarehouseManagement.Domain.Entity;
using WarehouseManagement.Infrstructre.Interface;

namespace WarehouseManagement.Application.CommandHandler.WarehouseSAppCommandHandler
{
    /// <summary>
    /// 确认入库列表中介者
    /// </summary>
    public class GetConfirmWarehouseentryCommandHandler : IRequestHandler<GetConfirmWarehouseentryCommand, WarehouseSAppPage>
    {
        private readonly lnterfaceWarehouseSAppRepository _warehouseSAppRepository;//仓库入库申请表
        private readonly lnterfaceStorageProductRepository _storageProductsRepository;//仓库入库申请产品明细表
        private readonly ISqlSugarClient db;

        public GetConfirmWarehouseentryCommandHandler(lnterfaceWarehouseSAppRepository warehouseSAppRepository, lnterfaceStorageProductRepository storageProductsRepository, ISqlSugarClient db)
        {
            _warehouseSAppRepository = warehouseSAppRepository;
            _storageProductsRepository = storageProductsRepository;
            this.db = db;
        }

        public async Task<WarehouseSAppPage> Handle(GetConfirmWarehouseentryCommand request, CancellationToken cancellationToken)
        {
            var list = db.Queryable<WarehouseSApp>()
                .LeftJoin<StorageProduct>((a, b) => a.StorageNumber == b.StorageNumber)
                .Where(a => a.IsDel == false && (a.State == 2 || a.State == 3))
                .GroupBy(a => new { a.ShippRequestAppId, a.StorageSource, a.StorageTopic, a.StorageNumber, a.AppName, a.AppDate, a.StorageType, a.WName, a.State })
                .Select((a, b) => new WarehouseSAppSelectDto
                {
                    ShippRequestAppId = a.ShippRequestAppId,
                    StorageSource = a.StorageSource,
                    StorageTopic = a.StorageTopic,
                    StorageNumber = a.StorageNumber,
                    AppName = a.AppName,
                    AppDate = a.AppDate,
                    StorageType = a.StorageType,
                    WName = a.WName,
                    State = a.State,
                    SPConfirmNum = SqlFunc.AggregateSum(b.SPConfirmNum),
                    SumNum = SqlFunc.AggregateSum(b.SumNum),

                }).ToList();
            if (!string.IsNullOrEmpty(request.WName))
            {
                list = list.Where(x => x.WName == request.WName).ToList();
            }
            if (!string.IsNullOrEmpty(request.StorageNumber))
            {
                list = list.Where(x => x.StorageNumber == request.StorageNumber).ToList();
            }
            if (!string.IsNullOrEmpty(request.StorageType))
            {
                list = list.Where(x => x.StorageType == request.StorageType).ToList();
            }
            if (!string.IsNullOrEmpty(request.Kdate))
            {
                list = list.Where(x => x.AppDate >= Convert.ToDateTime(request.Kdate)).ToList();
            }
            if (!string.IsNullOrEmpty(request.Sdate))
            {
                list = list.Where(x => x.AppDate <= Convert.ToDateTime(request.Sdate)).ToList();
            }
            WarehouseSAppPage page = new WarehouseSAppPage();
            page.SumCount = list.Count();
            page.PageLast = (int)Math.Ceiling(page.SumCount * 1.0 / request.PageSize);
            page.list = list.Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            return page;
        }
    }
}
